package com.home.retrofit

import MyLoggingInterceptor
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient.Builder
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.logging.HttpLoggingInterceptor
import okhttp3.logging.HttpLoggingInterceptor.Level.BODY
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit.SECONDS

class WXXWRetrofitUtil {
    companion object {
        fun getWXXWApi(): WXXWApi {

            val loggingInterceptor = HttpLoggingInterceptor { message -> //打印retrofit日志
                println("zzm debug retrofitBack = $message")
            }
            loggingInterceptor.setLevel(BODY)

            var mTimeOut = 1L
            var client = Builder()
                // .cache(RepositoryId.cache)
                .addInterceptor(loggingInterceptor)
                // .addInterceptor(MyLoggingInterceptor)
                .connectTimeout(mTimeOut, SECONDS)
                .readTimeout(mTimeOut, SECONDS)
                .writeTimeout(mTimeOut, SECONDS)
                .build()

            return Retrofit.Builder()
                .baseUrl("http://172.16.10.82")
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(WXXWApi::class.java)
        }

        fun str2RequestBody(str: String): RequestBody =
            str.toRequestBody("text/plain".toMediaType())
    }
}